﻿//==================================================================
// Copyright (C) 2011 深圳市中旭企业管理股份有限公司
// 作 者：江怀玉
// Email:iSunStudio@hotmail.com
// 日 期：2011/10/19
// 功能:完成界面控件信息和实体对象的值的影射!
//		 通过影射信息来完成控件值和实体的的值
//		 传递和转换.
// 版 本：1.00
// 修改历史纪录
// 版 本  修改时间      修改人            修改内容
//==================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ZX.Common;
using ZX.DBModule;

namespace ZX.Mapping
{

    /// <summary>
    /// 
    /// </summary>
    public class WebUiEnMapper : IMapping
    {
        public WebUiEnMapper(object entity, object page)
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
            this.Entitys = entity;
            this.Pages = page;
        }

        public WebUiEnMapper()
        {
        }


        /// <summary>
        /// 通过控件值付值对实体对象。
        /// </summary>
        /// <returns></returns>
        public void UI2EN()
        {
            if (this._Entitys == null || this._Pages == null)
                return; 

            SortedDictionary<string,object> _ht = ZX.Common.Reflection.ReaderAutoMapper(this.Entitys);
            SortedDictionary<string,object>.Enumerator myEnumerator = _ht.GetEnumerator();

            while (myEnumerator.MoveNext())
            {
                try
                {
                    string _name = myEnumerator.Current.Key.ToString();
                    System.Web.UI.Control _ctrl = this.GetControl(myEnumerator.Current.Key.ToString()) as System.Web.UI.Control;
                    Globals.SetPropertyValue(this.Entitys, _name, this.GetValue(_ctrl));
                }
                catch (System.Exception e)
                {
                    // return false;
                }
            }
        }

        /// <summary>
        /// 通实体值对付值给控件对象
        /// </summary>
        /// <returns></returns>
        public void EN2UI()
        {
            if (this._Entitys == null || this._Pages == null)
                return;
            SortedDictionary<string, object> _ht = ZX.Common.Reflection.ReaderAutoMapper(this.Entitys);
            SortedDictionary<string, object>.Enumerator myEnumerator = _ht.GetEnumerator();

            while (myEnumerator.MoveNext())
            {
                string _name = myEnumerator.Current.Key.ToString();
                System.Web.UI.Control _ctrl = new System.Web.UI.Control();
                try
                {
                    _ctrl = this.GetControl(myEnumerator.Current.Key.ToString()) as System.Web.UI.Control;
                }
                catch
                {

                }
                if (_ctrl != null)
                {
                    try
                    {
                        SetValue(_ctrl, Globals.GetPropertyValue(this.Entitys, _name));
                    }
                    catch { }
                }
            }
        }


        /// <summary>
        /// 对控件设值
        /// </summary>
        /// <param name="c">控件</param>
        /// <param name="values">值</param>
        private void SetValue(object c, object pvalues)
        {

            if (c == null || pvalues == null)
            {
                return;
            }
            string values = string.Empty;
            if (pvalues is System.DateTime)
            {
                values = ((System.DateTime)pvalues).ToShortDateString();
            }
            else
            {
                values = pvalues.ToString();
            }

            if (c is System.Web.UI.WebControls.TextBox)
            {
                System.Web.UI.WebControls.TextBox _C = c as System.Web.UI.WebControls.TextBox;
                _C.Text = values.ToString();
                return;
            }

            if (c is System.Web.UI.WebControls.DropDownList)
            {
                System.Web.UI.WebControls.DropDownList _C = c as System.Web.UI.WebControls.DropDownList;
                try
                {
                    _C.SelectedValue = values.ToString();
                }
                catch (System.Exception e)
                {
                    // log.write(e);
                }
            }

            //if (c is IT9D.TinyMCEWrapper.TinyMCE)
            //{
            //    IT9D.TinyMCEWrapper.TinyMCE _C = c as IT9D.TinyMCEWrapper.TinyMCE;
            //    _C.Text = values.ToString();
            //}

            //if (c is IT9D.Controls.ResourceCheckBox)
            //{
            //    IT9D.Controls.ResourceCheckBox _C = c as IT9D.Controls.ResourceCheckBox;
            //    _C.Checked = values.ToString()=="1"?true:false;
            //}

            if (c is System.Web.UI.WebControls.DropDownList)
            {
                System.Web.UI.WebControls.DropDownList _C = c as System.Web.UI.WebControls.DropDownList ;
                _C.SelectedValue = values.ToString();
            }

            if (c is System.Web.UI.WebControls.Label)
            {
                System.Web.UI.WebControls.Label _C = c as System.Web.UI.WebControls.Label;
                _C.Text = values.ToString();
            }

        }



        /// <summary>
        /// 获取控件值
        /// </summary>
        /// <param name="c">控件对象</param>
        /// <returns>返回控件值</returns>
        private string GetValue(object c)
        {
            if (c == null)
            {
                return "";
            }

            if (c is System.Web.UI.WebControls.TextBox)
            {
                System.Web.UI.WebControls.TextBox _C = c as System.Web.UI.WebControls.TextBox;
                if (_C != null)
                {
                    return _C.Text;
                }
            }

            if (c is System.Web.UI.WebControls.DropDownList)
            {
                System.Web.UI.WebControls.DropDownList _C = c as System.Web.UI.WebControls.DropDownList;
                if (_C != null)
                {
                    return _C.SelectedValue;
                }
            }

            if (c is System.Web.UI.HtmlControls.HtmlInputText)
            {
                System.Web.UI.HtmlControls.HtmlInputText _C = c as System.Web.UI.HtmlControls.HtmlInputText;
                if (_C != null)
                {
                    return _C.Value;
                }
            }

            if (c is System.Web.UI.WebControls.CheckBox)
            {
                System.Web.UI.WebControls.CheckBox _C = c as System.Web.UI.WebControls.CheckBox;
                if (_C != null)
                {
                    return _C.Text;
                }
            }


            //if (c is IT9D.TinyMCEWrapper.TinyMCE)
            //{
            //    IT9D.TinyMCEWrapper.TinyMCE _C = c as IT9D.TinyMCEWrapper.TinyMCE;
            //    return _C.Text;
            //}

            if (c is System.Web.UI.WebControls.DropDownList)
            {
                System.Web.UI.WebControls.DropDownList _C = c as System.Web.UI.WebControls.DropDownList;
                return _C.SelectedValue;
            }

            //if (c is IT9D.Controls.ResourceCheckBox)
            //{
            //    IT9D.Controls.ResourceCheckBox _C = c as IT9D.Controls.ResourceCheckBox;
            //    return (_C.Checked ? 1 : 0).ToString();
            //}

            return string.Empty;
        }


        /// <summary>
        /// 通过控件ID获取控件对象
        /// </summary>
        /// <param name="ctrlid">控件ID</param>
        /// <returns>控件对象</returns>
        private object GetControl(string ctrlid)
        {
            object control = null;
            if (this.Pages is System.Web.UI.Page)
            {
                control = (this.Pages as System.Web.UI.Page).FindControl(ctrlid);

            }
            if (this.Pages is System.Web.UI.UserControl)
            {
                control = (this.Pages as System.Web.UI.UserControl).FindControl(ctrlid);
            }

            //如果没有该控件抛出错误
            if (control == null)
            { }
                //throw new ArgumentNullException(string.Format("The page {0} has no control:{1}", page.Request.Url, ctrlid));
            return control;
        }


        /// <summary>
        /// 实体类
        /// </summary>
        private object _Entitys;
        public object Entitys
        {
            set { _Entitys = value; }
            get { return _Entitys; }
        }

        /// <summary>
        /// 当前控件容器，通过它搜索对应的值控件，并获取其属性信息及值！
        /// </summary>
        private object _Pages;
        public object Pages
        {
            set { _Pages = value; }
            get { return _Pages; }
        }
    }

}
